<template>
  <div class="attachment-preview" v-if="attachments.length > 0">
    <div class="preview-header">
      <span class="preview-title">已选择 {{ attachments.length }} 个附件</span>
      <a-button type="link" size="small" @click="clearAll">
        清空全部
      </a-button>
    </div>
    <div class="preview-grid">
      <div 
        v-for="(item, index) in attachments" 
        :key="index"
        class="preview-item"
        :class="{ 'is-image': item.type === 'image' }"
      >
        <!-- 图片预览 -->
        <div v-if="item.type === 'image'" class="image-wrapper">
          <a-image 
            :src="item.preview" 
            :alt="item.name" 
            class="preview-image"
            :preview="{
              mask: '点击预览'
            }"
          />
          <div class="image-overlay">
            <a-button 
              type="text" 
              size="small" 
              class="remove-btn"
              @click.stop="removeItem(index)"
            >
              <CloseOutlined />
            </a-button>
          </div>
        </div>
        
        <!-- 文件预览 -->
        <div v-else class="file-wrapper" :class="{ 'is-extracting': item.extracting, 'has-error': item.error }">
          <div class="file-icon">
            <LoadingOutlined v-if="item.extracting" spin />
            <ExclamationCircleOutlined v-else-if="item.error" />
            <CheckCircleOutlined v-else-if="item.extractedText" />
            <FileOutlined v-else />
          </div>
          <div class="file-info">
            <div class="file-name" :title="item.name">{{ item.name }}</div>
            <div class="file-status">
              <span v-if="item.extracting" class="status-text">提取中...</span>
              <span v-else-if="item.error" class="status-text error">{{ item.error }}</span>
              <span v-else-if="item.extractedText" class="status-text success">已提取</span>
              <span v-else class="file-size">{{ formatSize(item.size) }}</span>
              <a-button
                v-if="item.extractedText"
                type="link"
                size="small"
                @click="previewContent(item)"
                class="preview-btn"
              >
                <EyeOutlined /> 预览内容
              </a-button>
              <a-button
                v-if="item.hasVisualization && item.extractedText"
                type="link"
                size="small"
                @click="showVisualization(item.visualizationData)"
                class="viz-btn"
              >
                <FileSearchOutlined /> 可视化
              </a-button>
            </div>
          </div>
          <a-button 
            type="text" 
            size="small" 
            class="remove-btn"
            @click="removeItem(index)"
            :disabled="item.extracting"
          >
            <CloseOutlined />
          </a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { 
  CloseOutlined, 
  FileOutlined, 
  LoadingOutlined, 
  CheckCircleOutlined, 
  ExclamationCircleOutlined,
  EyeOutlined,
  FileSearchOutlined
} from '@ant-design/icons-vue';
import { Modal } from 'ant-design-vue';
import { h } from 'vue';

const props = defineProps({
  attachments: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['remove', 'clear', 'showVisualization', 'previewContent']);

const removeItem = (index) => {
  emit('remove', index);
};

const clearAll = () => {
  emit('clear');
};

const showVisualization = (visualizationData) => {
  emit('showVisualization', visualizationData);
};

const previewContent = (item) => {
  const content = h('div', {
    style: {
      maxHeight: '500px',
      overflowY: 'auto',
      padding: '8px',
      whiteSpace: 'pre-wrap',
      wordBreak: 'break-word',
      fontSize: '13px',
      lineHeight: '1.6',
      backgroundColor: '#f5f5f5',
      borderRadius: '4px'
    }
  }, item.extractedText || '暂无内容');

  Modal.info({
    title: `预览：${item.name}`,
    width: 700,
    content: content,
    okText: '关闭',
    icon: h(FileOutlined)
  });
};

const formatSize = (bytes) => {
  if (bytes < 1024) return bytes + ' B';
  if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
  return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
};
</script>

<style lang="less" scoped>
.attachment-preview {
  width: 100%;
  padding: 12px;
  background: var(--gray-20);
  border-radius: 8px;
  margin-bottom: 8px;

  .preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;

    .preview-title {
      font-size: 13px;
      color: var(--gray-700);
      font-weight: 500;
    }

    .ant-btn-link {
      padding: 0;
      height: auto;
      font-size: 12px;
      color: var(--gray-600);

      &:hover {
        color: var(--main-color);
      }
    }
  }

  .preview-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  .preview-item {
    position: relative;
    border-radius: 6px;
    overflow: hidden;
    background: white;
    border: 1px solid var(--gray-200);

    &.is-image {
      width: 100px;
      height: 100px;
      flex-shrink: 0;
    }

    &:not(.is-image) {
      width: 100%;
      flex-basis: 100%;
    }
  }

  .image-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    cursor: pointer;

    .preview-image {
      width: 100%;
      height: 100%;
      
      :deep(img) {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .image-overlay {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0);
      display: flex;
      align-items: flex-start;
      justify-content: flex-end;
      padding: 4px;
      opacity: 0;
      transition: all 0.2s ease;

      .remove-btn {
        background: rgba(0, 0, 0, 0.6);
        color: white;
        border-radius: 4px;
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0;

        &:hover {
          background: rgba(0, 0, 0, 0.8);
        }
      }
    }

    &:hover .image-overlay {
      opacity: 1;
      background: rgba(0, 0, 0, 0.1);
    }
  }

  .file-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    transition: all 0.3s;

    &.is-extracting {
      opacity: 0.7;
      
      .file-icon {
        color: var(--main-color);
      }
    }

    &.has-error {
      .file-icon {
        background: #fff2f0;
        color: #ff4d4f;
      }
    }

    .file-icon {
      flex-shrink: 0;
      width: 32px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: var(--gray-100);
      border-radius: 4px;
      font-size: 16px;
      color: var(--gray-600);
      transition: all 0.3s;
    }

    .file-info {
      flex: 1;
      min-width: 0;

      .file-name {
        font-size: 12px;
        color: var(--gray-900);
        font-weight: 500;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .file-status {
        margin-top: 2px;
        display: inline-flex;
        align-items: center;
        gap: 8px;

        .status-text {
          font-size: 11px;
          display: inline-flex;
          align-items: center;
          gap: 4px;
          flex-shrink: 0;
          
          &:not(.error):not(.success) {
            color: var(--gray-500);
          }

          &.error {
            color: #ff4d4f;
          }

          &.success {
            color: #52c41a;
          }
        }
        
        .preview-btn,
        .viz-btn {
          padding: 0 4px;
          height: auto;
          font-size: 11px;
          flex-shrink: 0;
        }

        .file-size {
          font-size: 11px;
          color: var(--gray-500);
        }
      }
    }

    .remove-btn {
      flex-shrink: 0;
      width: 20px;
      height: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0;
      color: var(--gray-500);

      &:hover {
        color: var(--gray-900);
      }

      &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
      }
    }
  }
}
</style>

